package com.jojo.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jojo.domain.Member;
import com.jojo.model.Result;
import com.jojo.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 后台管理系统维护会员控制层
 */
@Api("后台管理系统会员接口管理")
@RestController
@RequestMapping("admin/user")
public class SysMemberController {
    @Autowired
    private MemberService memberService;

    @ApiOperation("多条件分页查询会员")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:user:page')")
    public Result<Page<Member>> loadMemberPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) String nickName,
                                               @RequestParam(required = false) Integer status){
        Page<Member> page = new Page<>(current,size);
        page = memberService.page(page,new LambdaQueryWrapper<Member>()
                .like(StringUtils.hasText(nickName),Member::getNickName,nickName)
                .eq(ObjectUtil.isNotNull(status),Member::getStatus,status)
                .orderByDesc(Member::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("根据表示查询会员详情")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('admin:user:info')")
    public Result<Member> loadMemberInfoById(@PathVariable Long id){
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .select(Member::getId,Member::getOpenId,Member::getPic,Member::getNickName,Member::getStatus)
                .eq(Member::getId,id)
        );
        return Result.success(member);
    }

    @ApiOperation("修改会员状态")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:user:update')")
    public Result<?> modifyMemberStatus(@RequestBody Member member){
        member.setUpdateTime(new Date());
        boolean updated = memberService.updateById(member);
        return Result.handle(updated);
    }

    @ApiOperation("批量删除会员")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:user:delete')")
    public Result<?> removeMembers(@RequestBody List<Integer> ids){
        //创建会员对象集合
        List<Member> list = new ArrayList<>();
        //循环遍历id集合
        ids.forEach(id-> list.add(Member.builder().id(id).status(-1).build()));
        boolean removed = memberService.updateBatchById(list);
        return Result.handle(removed);
    }

    ///////////      feign接口  /////////////
    @GetMapping("getNickNameByOpenId")
    public Result<String> getNickNameByOpenId(@RequestParam String openId){
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .select(Member::getNickName)
                .eq(Member::getOpenId, openId)
        );
        return Result.success(member.getNickName());
    }

}
